package com.quanyue.music.system.service.impl;

import com.quanyue.auto.util.KeyGenerator;
import com.quanyue.music.system.dto.PlayListExcel;
import com.quanyue.music.system.dto.PlayListExcelResult;
import com.quanyue.music.system.service.AutoExcelService;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

@Service
public class AutoExcelServiceImpl implements AutoExcelService {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    public PlayListExcelResult readExcel(File file, String fileName) {
        Workbook wb;
        Sheet sheet;
        Row row;
        BufferedInputStream in;
        PlayListExcelResult resultTotal = new PlayListExcelResult();
        List <PlayListExcel> result = new ArrayList <>();
        String sessionId = KeyGenerator.uuid();
        try {
            String ext = fileName.substring(fileName.lastIndexOf("."));
            in = new BufferedInputStream(new FileInputStream(file));
            if (".xls".equals(ext)) {
                wb = new HSSFWorkbook(in);
            } else if (".xlsx".equals(ext)) {
                wb = new XSSFWorkbook(in);
            } else {
                wb = null;
            }
            sheet = wb.getSheetAt(0);
            // 得到总行数
            int rowNum = sheet.getLastRowNum();
            // 得到总列数
            int colNum;
            for (int i = 0; i <= rowNum; i++) {
                // 获取行对象
                row = sheet.getRow(i);
                colNum = row.getPhysicalNumberOfCells();
                int j = 0;
                PlayListExcel excel = new PlayListExcel();
                while (j < colNum) {
                    // 获取列对象
                    Object obj = getCellFormatValue(row.getCell(j));
                    if (j == 0) {
                        excel.setSegmentKey(obj.toString());
                    } else if (j == 1) {
                        excel.setInsertKey(obj.toString());
                    }
                    j++;
                }
                result.add(excel);
            }
            resultTotal.setPlayListExcels(result);
            resultTotal.setSessionId(sessionId);
            // 删除文件 关闭文件缓冲流
            file.delete();
            in.close();
            wb.close();
        } catch (FileNotFoundException fileException) {
            logger.error("FileNotFoundException: ", fileException);
        } catch (IOException io) {
            logger.error("IOException: ", io);
        }
        return resultTotal;
    }

    private Object getCellFormatValue(Cell cell) {
        Object cellvalue = "";
        if (cell != null) {
            // 判断当前Cell的Type
            switch (cell.getCellType()) {
                case Cell.CELL_TYPE_NUMERIC:// 如果当前Cell的Type为NUMERIC
                    cellvalue = (int) cell.getNumericCellValue();
                    break;
                case Cell.CELL_TYPE_STRING:// 如果当前Cell的Type为STRING
                    // 取得当前的Cell字符串
                    cellvalue = cell.getRichStringCellValue().getString();
                    break;
                default:// 默认的Cell值
                    cellvalue = "";
            }
        }
        return cellvalue;
    }
}
